/*
	HamsterBallModel
	The object for the ball which will be the player.
	
	possible args:
	radius:double
	latNum:int
	lonNum:int
*/
function HamsterBallModel (){
    setDefaults(this);
    this.radius = 1.5;
	var latNum = 50;
	var lonNum = 50;
	this.atEnd = false;
	
	this.stuck = false;
	
	var ballVertexData = [];
	var ballNormalData = [];
	var ballColorData = [];
	
	
	this.color = [1.4, 0.7, 0.7, 1.0];
	var velocity = vec3.create([0.0,0.0,0.0]);
	var accel = vec3.create([0.0,0.0,-0.010]);
	
	var prevPosition = [0.0,0.0,0.0];
	
	this.elasticity = 0.0;
	this.mass = 1.0;
	
	var light = vec3.create([0.0, 5.0, 0.0]);
	//y = y0 + r sin(theta) sin(phi)
	//x = x0 + r sin(theta) cos(phi)
	//x = x0 + r cos(theta)
	
	this.create = function(){
        handleHamsterBall(JSON.parse(hamsterball));
	}
	
	
	var vertexPositionBuffer;
    var vertexNormalBuffer;
    var vertexTextureCoordBuffer;
    var vertexIndexBuffer;
	var hamsterPositionBuffer;
	var hamsterNormalBuffer;
	var hamsterTextureBuffer;
	var hamsterTexture;
	var loaded = false;
	function handleHamsterBall(hamsterBallData){
		
		vertexNormalBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexNormalBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(hamsterBallData.objs[0].mesh.n), gl.STATIC_DRAW);
        vertexNormalBuffer.itemSize = 3;
        vertexNormalBuffer.numItems = hamsterBallData.objs[0].mesh.n.length / 3;

        vertexTextureCoordBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexTextureCoordBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(hamsterBallData.objs[0].mesh.f), gl.STATIC_DRAW);
        vertexTextureCoordBuffer.itemSize = 2;
        vertexTextureCoordBuffer.numItems = hamsterBallData.objs[0].mesh.f.length / 2;

        vertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(hamsterBallData.objs[0].mesh.v), gl.STATIC_DRAW);
        vertexPositionBuffer.itemSize = 3;
        vertexPositionBuffer.numItems = hamsterBallData.objs[0].mesh.v.length / 3;
		
		
        hamsterPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, hamsterPositionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-0.8, -0.8, 0.0, 
														0.8, -0.8, 0.0, 
														-0.8, 0.8, 0.0, 
														0.8, 0.8, 0.0]), gl.STATIC_DRAW);
        hamsterPositionBuffer.itemSize = 3;
        hamsterPositionBuffer.numItems = 4;
		
		hamsterNormalBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, hamsterNormalBuffer);
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0.0,0.0,1.0, 
														0.0,0.0,1.0, 
														0.0,0.0,1.0, 
														0.0,0.0,1.0]), gl.STATIC_DRAW);
		hamsterNormalBuffer.itemSize = 3;
		hamsterNormalBuffer.NumItems = 4;

		hamsterTextureBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, hamsterTextureBuffer);
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0.0, 0.0,
														1.0, 0.0,
														0.0, 1.0,
														1.0, 1.0]), gl.STATIC_DRAW)
		
        vertexIndexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(hamsterBallData.objs[0].mesh.f), gl.STATIC_DRAW);
        vertexIndexBuffer.itemSize = 1;
        vertexIndexBuffer.numItems = hamsterBallData.objs[0].mesh.f.length;
		/*hamsterTexture = gl.createTexture();
		hamsterTexture.image = new Image();
		hamsterTexture.image.onload = function() {
		  handleLoadedTexture(hamsterTexture)
		}*/

		//hamsterTexture.image.src = "bijou.gif";
		loaded = true;
	}
	
	function handleLoadedTexture(texture) {
		gl.bindTexture(gl.TEXTURE_2D, texture);
		gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
		gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
		gl.bindTexture(gl.TEXTURE_2D, null);
	}
	
	this.getVertexData = function(){
		return ballVertexData;	
	}
	
	this.getNormalData = function(){
		return ballNormalData;
	}

	var mvMatrix = mat4.create();
	var pMatrix = mat4.create();
	var mvMatrixStack = [];
	

	function mvPushMatrix() {
		var copy = mat4.create();
		mat4.set(mvMatrix, copy);
		mvMatrixStack.push(copy);
	}

	function mvPopMatrix() {
		if (mvMatrixStack.length == 0) {
			throw "Invalid popMatrix!";
		}
		mvMatrix = mvMatrixStack.pop();
	}
	

    function setMatrixUniforms(camera, shader, color) {
        gl.uniformMatrix4fv(shader.pMatrixUniform, false, camera);
        gl.uniformMatrix4fv(shader.mvMatrixUniform, false, mvMatrix);
		gl.uniform4fv(shader.aColor, color);	
    }

	var rotateBy = 0.0;
	var multiplier0 = -1;
    this.initBuffers = function() {
    }
	
    this.draw = function(camera) {
		if(!loaded)
			return;
		
        mat4.identity(mvMatrix);
        mat4.translate(mvMatrix, this.pos);
		//mat4.translate(mvMatrix, [0.0, this.radius, 0.0]);
		
		//Set up rotations
		var tempVec = vec3.create(0.0,0.0,0.0);
		var vecLength = 0.0;
		
		
		vec3.subtract(prevPosition, this.pos, tempVec);
		
		vecLength = vec3.length(tempVec);
		
		if(this.stuck){
			if((this.pos[0] - prevPosition[0]) > 0.0){
				multiplier0 = -1;
			}
			else{
				multiplier0 = 1;
			}
			
		}
		
		rotateBy += multiplier0 * ((Math.PI * 2) * (vecLength / (2.0 * Math.PI * this.radius)));
		rotateBy  = rotateBy  % (Math.PI * 2);
		mat4.rotate(mvMatrix, rotateBy, [0.0,0.0,1.0], mvMatrix);
		mat4.rotate(mvMatrix, 1.0, [1.0, 1.0, 1.0], mvMatrix);
		
		
		mat4.scale(mvMatrix, [this.radius, this.radius, this.radius], mvMatrix);		
		
		gl.useProgram(this.shader);
		
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
        gl.vertexAttribPointer(this.shader.vertexPositionAttribute, vertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
		
		gl.bindBuffer(gl.ARRAY_BUFFER, vertexNormalBuffer);
		gl.vertexAttribPointer(this.shader.vertexNormalAttribute, vertexNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);
		
		
		
		setMatrixUniforms(camera, this.shader, this.color);
        
        gl.drawElements(gl.TRIANGLES, vertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
		
		
		
		//Draw the hamster
		/*mat4.identity(mvMatrix);
		
        mat4.translate(mvMatrix, this.pos);
		mat4.scale(mvMatrix, [this.radius, this.radius, this.radius], mvMatrix);	
		
		gl.useProgram(this.shader);
		
		
		gl.bindBuffer(gl.ARRAY_BUFFER, hamsterPositionBuffer);
        gl.vertexAttribPointer(this.shader.vertexPositionAttribute, hamsterPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
		
		gl.bindBuffer(gl.ARRAY_BUFFER, hamsterNormalBuffer);
		gl.vertexAttribPointer(this.shader.vertexNormalAttribute, hamsterNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);
		
		gl.bindBuffer(gl.ARRAY_BUFFER, hamsterTextureBuffer);
		gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, hamsterTextureBuffer.itemSize, gl.FLOAT, false, 0, 0)
		
		gl.activeTexture(gl.TEXTURE0);
		gl.bindTexture(gl.TEXTURE_2D, hamsterTexture);
		gl.uniform1i(shaderProgram.samplerUniform, 0);
		
		setMatrixUniforms(camera, this.shader, this.color);
		gl.drawArrays(gl.TRIANGLE_STRIP, 0, hamsterPositionBuffer.numItems);*/
		
		prevPosition = this.pos;
		
    }


	
}
